<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\Role;
use App\Models\UserProcess;
use App\Models\Workshop;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(User::with(['workshop']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('workshop.title', admin_trans('user.fields.workshop_title'));
            $grid->column('role_ids')->display(function($role_ids) {
                $title = '';
                if ($role_ids) {
                    $role = Role::whereIn('id', $role_ids)->get();

                    foreach ($role as $key => $value) {
                        if ($key == 0) {
                            $title = $value['title'];
                        } else {
                            $title .= ",{$value['title']}";
                        }
                    }

                }

                return $title;
            });
            $grid->column('name');
            $grid->column('phone');
            $grid->column('status')->using(admin_trans('user.options.status'));
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, User::with(['workshop']), function (Show $show) {
            $show->field('id');
            $show->field('workshop.title', admin_trans('user.fields.workshop_title'));
            $show->field('role_ids')->as(function ($role_ids) {
                $title = '';
                $role = Role::whereIn('id', $role_ids)->get();

                foreach ($role as $key => $value) {
                    if ($key == 0) {
                        $title = $value['title'];
                    } else {
                        $title .= ",{$value['title']}";
                    }
                }

                return $title;
            });
            $show->field('number');
            $show->field('name');
            $show->field('phone');
            $show->field('status')->using(admin_trans('user.options.status'));
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->select('workshop_id')
                ->options(Workshop::where('status', DEFAULT_STATUS_SUCCESS)->pluck('title', 'id'))
                ->required();
            $form->multipleSelect('role_ids')->options(Role::where('status', DEFAULT_STATUS_SUCCESS)->pluck('title', 'id'))
                ->required();
            $form->text('number');
            $form->text('name');
            $form->text('phone');
            $form->password('password');
            $form->radio('status')->options(admin_trans('user.options.status'))->default('1');

            $form->saved(function (Form $form) {
                $id = $form->getKey();

                if ($form->role_ids && in_array(M01_ID, $form->role_ids)) {
                    $process = UserProcess::where('user_id', $id)->where('workshop_id', $form->workshop_id)->first();

                    if (!$process) {
                        UserProcess::create([
                            'user_id' => $id,
                            'workshop_id' => $form->workshop_id
                        ]);
                    } else {
                        $process->user_id = $id;
                        $process->workshop_id = $form->workshop_id;
                        $process->save();
                    }
                } else {
                    UserProcess::where('user_id', $id)->where('workshop_id', $form->workshop_id)->delete();
                }
            });
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
